﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data;

public partial class shop : System.Web.UI.Page
{
    static string x;
    static string y;
    static string place_id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            if((Request.QueryString["x"]!=null)&&(Request.QueryString["x"]!=string.Empty))
            {
                x=Request.QueryString["x"].ToString();
            }

            if((Request.QueryString["y"]!=null)&&(Request.QueryString["y"]!=string.Empty))
            {
                y=Request.QueryString["y"].ToString();
            }

            if ((Request.QueryString["place_id"] != null) && (Request.QueryString["place_id"] != string.Empty))
            {
                place_id = Request.QueryString["place_id"].ToString();
            }
        }
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        Boolean check;
        check=CheckInput();
        if (check == true)
        {
            //thuc hien luu du lieu vao bang tai day
            SavetoServer();
            int a = getShopActiveid();
            ClientScript.RegisterStartupScript(this.GetType(), "close win", "<script>addMaker("+place_id +","+ a +");</script>");
        }
        else
        {
            //do something
            ClientScript.RegisterStartupScript(this.GetType(), "display alert", "<script>DisplayeAlert();</script>");
        }
    }
    public ShopMap getInfoShopMap()
    {
        ShopMap a = new ShopMap();
        a.Name = BoxName.Text;
        a.Description = BoxDes.Text;
        a.Phone = Boxphone.Text;
        a.Address = BoxAddress.Text;
        a.Tell = BoxTel.Text;
        a.Image = "aaa";
        return a;
    }
    //hamm luu vao server 
    public void SavetoServer()
    {
        ShopMap shopMap = new ShopMap();
        shopMap  = getInfoShopMap();
        //khai bao chuoi truy van den server 
        String sql = "INSERT INTO tbshops(name,description,address,tel,phone,image,x,y,place_id) VALUES " +
            "(@name,@description,@address,@tel,@phone,@image,@x,@y,@place_id)";
        //thuc hien ket noi 
        ConnectionStringSettings settings = WebConfigurationManager.ConnectionStrings["shop_ConnectionString"];
        SqlConnection connection = new SqlConnection(settings.ConnectionString);
        try
        {
            connection.Open();
            SqlCommand cmd = new SqlCommand(sql, connection);
            SqlParameter[] param = new SqlParameter[9];//mang chua cac tham so truyen vao trong cau truy van 
            //dua cac du lieu vao mang 
            param[0] = new SqlParameter("@name", SqlDbType.NVarChar, 50);
            param[1] = new SqlParameter("@description", SqlDbType.NVarChar, 1000);        
            param[2] = new SqlParameter("@address", SqlDbType.NVarChar,50);
            param[3] = new SqlParameter("@tel", SqlDbType.NVarChar, 50);
            param[4] = new SqlParameter("@phone", SqlDbType.NVarChar, 50);
            param[5] = new SqlParameter("image", SqlDbType.VarChar, 100);
            param[6] = new SqlParameter("@x", SqlDbType.Text);
            param[7] = new SqlParameter("@y", SqlDbType.Text);
            param[8] = new SqlParameter("@place_id", SqlDbType.Int);
            //them cac gia tri 
            param[0].Value = shopMap.name;
            param[1].Value = shopMap.description;
            param[2].Value = shopMap.address;
            param[3].Value = shopMap.tell;
            param[4].Value = shopMap.phone;
            param[5].Value = shopMap.image;
            param[6].Value = y;
            param[7].Value = x;
            param[8].Value = place_id;

            for (int i = 0; i < param.Length; i++)
            {
                cmd.Parameters.Add(param[i]);
            }
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        catch (System.Data.SqlClient.SqlException e)
        {
            throw new Exception("Loi:"+e.Message);
        }
        finally
        {
            connection.Close();
            
        }
        //thuc hien them cac du lieu vao trong bang
    }
    public int getShopActiveid()
    {
        int count = 0;
        try
        {
            string sql = "SELECT COUNT(del_flag) FROM  tbShops WHERE (del_flag = 0 And place_id = " + place_id + ")";
            SqlConnection conn = OpenConnection();
            SqlCommand cmd = new SqlCommand(sql, conn);
            count = (Int32)cmd.ExecuteScalar();
            CloseConnection(conn);
            return count;
        }
        catch (System.Exception ex)
        {
            return count;
        }

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "close win", "<script>window.close()</script>");
    }

    //ham kiem tra ket qua nhap vao 
    public Boolean CheckInput()
    {
        if (
         BoxName.Text == string.Empty ||
         BoxDes.Text == string.Empty ||
         Boxphone.Text == string.Empty ||
         BoxTel.Text == string.Empty ||
         BoxAddress.Text == string.Empty)
            return false;
        else
            return true;

    }

    public static SqlConnection OpenConnection()
    {
        try
        {
            ConnectionStringSettings settings = WebConfigurationManager.ConnectionStrings["shop_ConnectionString"];
            SqlConnection connection = new SqlConnection(settings.ConnectionString);
            connection.Open();
            return connection;
        }
        catch (Exception)
        {
            return null;
        }
    }
    public static void CloseConnection(SqlConnection connection)
    {
        if (connection != null)
        {
            connection.Close();
        }
    }
}